/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.jiujiudevice.entity.JiuInkRecord;
import com.boxin.ims.modules.jiujiudevice.dao.JiuInkRecordDao;

/**
 * 墨盒使用记录Service
 * @author sxh
 * @version 2018-10-26
 */
@Component
@Transactional(readOnly = true)
public class JiuInkRecordService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuInkRecordService.class);
	
	@Autowired
	private JiuInkRecordDao jiuInkRecordDao;
	
	public JiuInkRecord get(Long id) {
		return jiuInkRecordDao.findOne(id);
	}
	
	public Page<JiuInkRecord> find(Page<JiuInkRecord> page, JiuInkRecord jiuInkRecord) {
		DetachedCriteria dc = jiuInkRecordDao.createDetachedCriteria();
		if(StringUtils.isNotEmpty(jiuInkRecord.getOrderNo())){
			dc.add(Restrictions.eq("orderNo", jiuInkRecord.getOrderNo()));
		}
		if(StringUtils.isNotEmpty(jiuInkRecord.getMemberId())){
			dc.add(Restrictions.eq("memberId", jiuInkRecord.getMemberId()));
		}
		
		if(StringUtils.isNotEmpty(jiuInkRecord.getSn())){
			dc.add(Restrictions.like("sn", jiuInkRecord.getSn()));
		}
		
		dc.addOrder(Order.desc("orderNo"));
		return jiuInkRecordDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(JiuInkRecord jiuInkRecord) {
		jiuInkRecordDao.save(jiuInkRecord);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuInkRecordDao.deleteById(id);
	}

	/***
	 * 
	 * @function: <p> 根据订单号和会员的openId得到墨盒的记录列表</p>
	 * @param orderNo
	 * @param memberId
	 * @return
	 */
	public List<JiuInkRecord> findByOrderNoAndDeviceSn(String orderNo, String sn) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuInkRecordDao.createDetachedCriteria();
		if(StringUtils.isNotEmpty(orderNo)){
			dc.add(Restrictions.eq("orderNo", orderNo));
		}
		if(StringUtils.isNotEmpty(sn)){
			dc.add(Restrictions.eq("sn", sn));
		}
		return null != jiuInkRecordDao.find(dc) && jiuInkRecordDao.find(dc).size()>0 ? jiuInkRecordDao.find(dc) :null;
	}
	
}
